package com.linewell.controller;

import com.linewell.entity.*;
import com.linewell.service.IArticleService;
import com.linewell.service.ICommentService;
import com.linewell.service.ITagService;
import com.linewell.service.IUserService;
import com.linewell.utils.Msg;
import com.linewell.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/page/news")
public class backController {

    @Autowired
    IArticleService articleServices;

    @Autowired
    IUserService userService;

    @Autowired
    ITagService tagService;

    @Autowired
    ICommentService commentService;


    /**
     * @descripition 跳转用户个人信息页面
     * @author hyuhuang@linewell.com
     * @date 2018/4/5 10:55
     * @return 指定转向userInfo.jsp
     */
    @RequestMapping("/userInfo")
    public String userInfo(){
        return "userInfo";
    }


    /**
     * @descripition 跳转文章审核信息页面
     * @author hyuhuang@linewell.com
     * @date 2018/4/5 12:55
     * @return 指定转向check.jsp
     */
    @RequestMapping("/check")
    public String check(){
        return "check";
    }
    /**
     * @descripition 跳转评论审核信息页面
     * @author hyuhuang@linewell.com
     * @date 2018/4/5 12:55
     * @return 指定转向check.jsp
     */
    @RequestMapping("/replyList")
    public String replyList(){
        return "replyList";
    }


    /**
     * @descripition 跳转修改密码页面
     * @author hyuhuang@linewell.com
     * @date 2018/4/5 14:22
     * @return 指定转向changePwd.jsp
     */
    @RequestMapping("/changePwd")
    public String changePwd(){
        return "changePwd";
    }


    /**
    * @descripition 跳转到文章管理页面
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param
    * @return java.lang.String
    */
    @RequestMapping("/newsList")
    public String articleManage(){
        return "newsList";
    }
    /**
     * @Description 挑选未审核的文章
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param
     * @param: page
     * @param: limit
     * @return java.util.Map<java.lang.String , java.lang.Object>
     */
    @RequestMapping("/searchAllArticleIsCheck")
    @ResponseBody
    public Map<String, Object> searchAllArticleIsCheck(@RequestParam("page") int page, @RequestParam("limit")int limit){
        int count = articleServices.getCountIsCheck();
        List<ArticleVo> list = articleServices.searchAllArticleIsCheck(page,limit);
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("code",0);
        map.put("msg","success");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
    /**
     * @Description 显示所有文章信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param
     * @param: page
     * @param: limit
     * @param: request
     * @return java.util.Map<java.lang.String , java.lang.Object>
     */
    @RequestMapping("/searchAllArticle")
    @ResponseBody
    public Map<String, Object> searchAllArticle(@RequestParam("page") int page, @RequestParam("limit")int limit, HttpServletRequest request){
        int count = articleServices.getCount();
        String keyword = request.getParameter("key");
        if (keyword == "" || keyword == null){
            List<ArticleVo> list = articleServices.searchAllArticle(page,limit);
            Map<String, Object> map = new HashMap<String, Object>(1);
            map.put("code",0);
            map.put("msg","success");
            map.put("count",count);
            map.put("data",list);
            return map;
        } else {
            List<ArticleVo> list = articleServices.searchArticleKey(keyword);
            Map<String, Object> map = new HashMap<String, Object>(1);
            map.put("code",0);
            map.put("msg","success");
            map.put("count",count);
            map.put("data",list);
            return map;
        }

    }

    /**
     * @Description 跳转页面
     * @author hyuhuang@linewell.com
     * @date 2018/04/21
     */
    @RequestMapping("/show")
    public String show(@RequestParam("articleId") Long articleId,Model model){
        Article article = articleServices.selectArticleWithArticleId(articleId);
        List<Tag> tags = tagService.getTagByArticle(article);
        model.addAttribute("article",article);
        model.addAttribute("tags",tags);
        return "detail";
    }
    /**
     * @Description 更新文章审核状态
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param: articleId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/checkArticle")
    @ResponseBody
    public Msg checkArticle(@RequestParam("articleId") Long articleId){
        return articleServices.updataArticleCheck(articleId);
    }
    /**
     * @Description 删除文章
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param
     * @param: articleId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/deleteArticleByArticleId")
    @ResponseBody
    public Msg deleteArticleByArticleId(@RequestParam("articleId") Long articleId){
        return articleServices.deleteArticleByArticleId(articleId);
    }
    /**
     * @Description 删除用户
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param
     * @param: userId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/deleteUserByUserId")
    @ResponseBody
    public Msg deleteUserByUserId(@RequestParam("userId") Long userId){
        return userService.deleteUserByUserId(userId);
    }

    /**
     * @Description 更新文章信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/updateArticle")
    @ResponseBody
    public Msg updateArticle(@RequestParam("field") String field,@RequestParam("value") String value,@RequestParam("articleId") Long articleId){
        return articleServices.updateArticle(StringUtil.upToLow(field),value,articleId);
    }
    /**
     * @Description 批量删除文章信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/deleteAllArticle")
    @ResponseBody
    public Msg deleteAllArticle(@RequestParam("articlesId") String articlesId){
        String articleIds = articlesId.substring(1);
        return articleServices.deleteAllArticle(articleIds);
    }
    /**
     * @Description 批量删除用户信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/deleteAllUser")
    @ResponseBody
    public Msg deleteAllUser(@RequestParam("userId") String userId){
        String userIds = userId.substring(1);
        return userService.deleteAllUser(userIds);
    }
    /**
    * @descripition 跳转到用户管理页面
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @return
    */
    @RequestMapping("/userList")
    public String userList(){
        return "userList";
    }

    /**
     * @Description 更新用户状态
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/updateUserState")
    @ResponseBody
    public Msg updateUserState(@RequestParam("userId")Long userId,@RequestParam("state")int state){
        if (state == 1) {
            state = 0;
        } else {
            state = 1 ;
        }
        return userService.updateUserState(userId,state);
     }

    /**
     * @Description 获取用户信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/getUsers")
    @ResponseBody
    public Map<String, Object> getUsers(@RequestParam("page") int page, @RequestParam("limit")int limit,HttpServletRequest request){
        int count = userService.getUserCount();
        String keyword = request.getParameter("key");
        List<UserInfo> list;
        if (keyword == "" || keyword == null){
            list = userService.getUsers(page,limit);
        } else {
            list = userService.getUsersByPunsterName(keyword);
        }
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("code",0);
        map.put("msg","success");
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    /**
     * @Description 更新用户
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Msg updateUser(@RequestParam("field") String field,@RequestParam("value") String value,@RequestParam("userId") Long userId){
        return userService.updateUser(StringUtil.upToLow(field),value,userId);
    }

    @RequestMapping("/getReplyList")
    @ResponseBody
    public Map<String, Object> getReplyList(@RequestParam("page") int page, @RequestParam("limit")int limit,HttpServletRequest request){
        int count =commentService.getReplyCount();
        List<ReplyVo> list = commentService.getReplyList(page,limit);
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("code",0);
        map.put("msg","success");
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/checkReply")
    @ResponseBody
    public Msg checkReply(@RequestParam("replyId") Long replyId){
        return commentService.updataReplyCheck(replyId);
    }

    @RequestMapping("/deleteReplyByReplyId")
    @ResponseBody
    public Msg deleteReplyByReplyId(@RequestParam("replyId") Long replyId){
        return commentService.deleteReplyByReplyId(replyId);
    }

    @RequestMapping("/updataAllReply")
    @ResponseBody
    public Msg updataAllReply(@RequestParam("replyId") String replyId){
        String replyIds = replyId.substring(1);
        return commentService.updataAllReply(replyIds);
    }

}
